import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/index',
      name: 'index',
      component: HomeView
    },
    {
      path: '/login',
      name: 'login',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/LoginView.vue')
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('../views/UserView.vue')
    }
  ]
})


// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 在每次路由跳转之前执行一些操作
  console.log('Before navigating to', to.path, from)  
  // 继续导航
  next()
})

// 全局后置钩子
router.afterEach((to, from) => {
  // 在每次路由跳转之后执行一些操作
  console.log('After navigating to', to.path, from)
})

export default router
